using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserPropertyDA {

        private const string SP_CountByReceiverUserKeyStatus = "UserProperty_CountByReceiverUserKeyStatus";
        private const string SP_GetByReceiverUserKeyStatusWithPaging = "UserProperty_GetByReceiverUserKeyStatusWithPaging";
        private const string SP_Insert = "UserProperty_Insert";
        private const string SP_UpdateStatus = "UserProperty_UpdateStatus";

		public static UserPropertyEnt CreateFromReader(IDataReader reader) {
			UserPropertyEnt item = new UserPropertyEnt();
			item.ID = ConvertUtils.ToLong(reader["ID"]);
			item.GiverUserKey = ConvertUtils.ToString(reader["GiverUserKey"]);
			item.ReceiverUserKey = ConvertUtils.ToString(reader["ReceiverUserKey"]);
			item.PropertyID = ConvertUtils.ToInt(reader["PropertyID"]);
			item.Message = ConvertUtils.ToString(reader["Message"]);
			item.Status = (EUserPropertyStatus)ConvertUtils.ToByte(reader["Status"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			return item;
        }

        public static int CountByReceiverUserKeyStatus(string receiverUserKey, EUserPropertyStatus status, string connectionName) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountByReceiverUserKeyStatus)) {
                db.AddParameter(cmd, "ReceiverUserKey", receiverUserKey);
                db.AddParameter(cmd, "Status", (byte)status);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static List<UserPropertyEnt> GetByReceiverUserKeyStatusWithPaging(string receiverUserKey, EUserPropertyStatus status, long startRow, long endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserPropertyEnt> result = new List<UserPropertyEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByReceiverUserKeyStatusWithPaging)) {
                db.AddParameter(cmd, "ReceiverUserKey", receiverUserKey);
                db.AddParameter(cmd, "Status", (byte)status);
                db.AddParameter(cmd, "StartRow", startRow);
                db.AddParameter(cmd, "EndRow", endRow);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }

        public static int Insert(UserPropertyEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
            Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameterOuter(cmd, "ID", DbType.Int64);
				db.AddParameter(cmd, "GiverUserKey", item.GiverUserKey);
				db.AddParameter(cmd, "ReceiverUserKey", item.ReceiverUserKey);
				db.AddParameter(cmd, "PropertyID", item.PropertyID);
				db.AddParameter(cmd, "Message", item.Message);
				db.AddParameter(cmd, "Status", item.Status);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
					item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
					item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
				}
			}
			return result;
		}

        public static int UpdateStatus(UserPropertyEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
            Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateStatus)) {
				db.AddParameter(cmd, "ID", item.ID);
                db.AddParameter(cmd, "ReceiverUserKey", item.ReceiverUserKey);
				db.AddParameter(cmd, "Status", item.Status);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}